define(function(require) {

  var $ = require("jquery");
  var _ = require("underscore");
  var Backbone = require("backbone");
  var MyModel = require("models/MyModel");
  var Utils = require("utils");
  var Parse = require("parse");

  var pictureSource = navigator.camera.PictureSourceType;   // picture source
  var destinationType = navigator.camera.DestinationType; // sets the format of returned value


  var InserisciAnnuncio = Utils.Page.extend({

    constructorName: "InserisciAnnuncio",

    model: MyModel,

    initialize: function() {
        // load the precompiled template
        this.template = Utils.templates.inserisciannuncio;
        // here we can register to inTheDOM or removing events
        // this.listenTo(this, "inTheDOM", function() {
        //   $('#content').on("swipe", function(data){
        //     console.log(data);
        //   });
        // });
        // this.listenTo(this, "removing", functionName);

        // by convention, all the inner views of a view must be stored in this.subViews

    },

    id: "searchview",
    className: "i-g page",

    events: {
      "touchend #inviaAnnuncio": "addImage",
      "touchend #capturePhoto" : "capturePhoto",
      "touchend #capturePhotoEdit" : "capturePhotoEdit",
      "touchend #getPhoto" : "getPhoto"
    },


    onPhotoDataSuccess: function(imageData) {

        var smallImage = document.getElementById('smallImage');

        // Unhide image elements
        //
        smallImage.style.display = 'block';

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        smallImage.src = "data:image/jpeg;base64," + imageData;

        console.log("imageData" + imageData);
    },

    onPhotoURISuccess: function(imageURI){
        
        console.log("imageURI  " + imageURI);
        
        var largeImage = document.getElementById('largeImage');
        // Unhide image elements
        //
        largeImage.style.display = 'block';

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        largeImage.src = imageURI;

        alert("mo ci provo");
        
        
        var parseFile = new Parse.File("mypic.jpg", {base64:imageURI});
        //var parseFile = new Parse.File("myfile.jpg", imageURI, "image/jpg");
        console.log(parseFile);
        
        var prodotto = new Parse.Object("Cesidio");

        parseFile.save().then(function() {
              
              prodotto.set("Descrizione", "Joe Smith");
              prodotto.set("Titolo", "prova");
              prodotto.set("Immagine", parseFile );
              prodotto.set("ciao", imageURI);
              prodotto.save();

          }, function(error) {
            console.log("Errore salvataggio parse");
            console.log(error);
        });


        alert("in teoria ho salvato");

        
        //var profilePhoto = prodotto.get("Immagine");
        //$("profileImg")[0].src = profilePhoto.url();
        //alert("url foto:" + profilePhoto.url() );

    },

    capturePhoto: function(events){
        //alert(events.target.attributes[1].value);
        // Retrieve image file location from specified source
        navigator.camera.getPicture(this.onPhotoURISuccess, this.onFail, { quality: 50,
        destinationType: destinationType.DATA_URL,
        sourceType: pictureSource.PHOTOLIBRARY});
        //sourceType: events.target.attributes[1].value }
    },

    capturePhotoEdit: function(){
        // Take picture using device camera, allow edit, and retrieve image as base64-encoded string
        navigator.camera.getPicture(this.onPhotoDataSuccess, this.onFail, { quality: 20, allowEdit: true,
        destinationType: destinationType.DATA_URL });
    },

    onFail: function(message){
        alert('Failed because: ' + message);
    },

    addImage: function(){
      
      var fileUploadControl = $("#profilePhotoFileUpload")[0];
      var file = fileUploadControl.files[0];


      document.getElementById('provaFile').innerHTML = file;
       //console.log("sto calcolando" + document.getElementById('profilePhotoFileUpload').files);
      var prova = document.getElementById('profilePhotoFileUpload').files;
      //console.log($.parseJSON(JSON.stringify(prova)));
      var name = document.getElementById('profilePhotoFileUpload').value.substr(12);
      
      alert("nome vale: " + $('input[type=file]').val() );


      if (fileUploadControl.files.length > 0) {
          
          var parseFile = new Parse.File(name, file);

          parseFile.save();

          var prodotto = new Parse.Object("Cesidio");
          prodotto.set("Descrizione", "Joe Smith");
          prodotto.set("Titolo", "prova");
          prodotto.set("Immagine", parseFile );
          prodotto.save();


      }

      this.render();

    },

    render: function() {
      $(this.el).html(this.template());
      return this;
    },

    goToSearch: function(e) {
      
      alert ("sto dentro goToSearch di SearchView!");
      Backbone.history.navigate("searchView", {
        trigger: true
      });
     
    }
  });

  return InserisciAnnuncio;

});